--[[
  杭州银行 - 直销银行WEB
]]
require("apm_core")
local ApmJson = require("ApmJson")
local ApmHttpUtil = require("ApmHttpUtil")
require("extension")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local t_xml_tree
local t_json_tree
local s_channel
local s_channel_name
local s_flowno
local s_rspcode
local s_rspmsg

local function detect_resp(s_payload)
    if s_payload == nil then return false end
    t_json_tree = ApmJson.decode(s_payload)
    return t_json_tree ~= nil
end

--- 解析响应
-- return
-- true: 解析成功; false: 解析失败
local function parse_resp()
    -- 交易返回码
    s_rspcode = t_json_tree.errorCode
    -- 交易返回信息
    s_rspmsg = t_json_tree.errorMessage
    return true
end

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end

    local s_url_params = ApmData.getField("request.http.param.data", 1)
    if s_url_params ~= nil and s_url_params ~= "" then
        local t_kv = ApmHttpUtil.parseParams(s_url_params)
        if t_kv ~= nil then
            local s_clientip = t_kv["clientip"]
            if s_clientip ~= nil then
                ApmData.setSrcIp(s_clientip)
            end
        end
    end

    if ApmData.getMsgType() ~= 1 then return end

    local s_resp

    local url = ApmData.getField("transaction.title", 1)
    if url == nil then return end
    -- 交易类型
    local _, _, s_transtype = string.find(url, "/(%a+)$")
    if s_transtype == nil then return end
    local s_transtype_name = s_transtype

    s_resp = ApmData.getField("response.http.body.1", 1)
    if s_resp ~= nil then
        s_resp = string.trim(s_resp)
        if detect_resp(s_resp) then
            parse_resp()
        end
    end

    ApmData.replaceField("transaction.type_code", s_transtype)
    ApmData.replaceField("transaction.type_name", s_transtype_name)

    if s_channel ~= nil then
        ApmData.replaceField("transaction.channel_code", s_channel)
        ApmData.replaceField("transaction.channel_name", s_channel_name)
    end

    if s_flowno ~= nil then
        ApmData.replaceField("transaction.code", s_flowno)
    end

    if s_rspcode ~= nil then
        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")
    end

    if s_rspmsg ~= nil then
        ApmData.replaceField("response.error_message", s_rspmsg)
    end

    -- 为了降低ES的压力，返回码正常的情况下删除body内容
--    if s_rspcode ~= "0" and s_rspcode ~= "00" and s_rspcode ~= "000000" then
--        ApmData.delFields("request.http.body.1")
--        ApmData.delFields("response.http.body.1")
--    end

    logInfo("直销WEB(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
        ApmData.getSrcIp(),
        ApmData.getSrcPort(),
        ApmData.getDstIp(),
        ApmData.getDstPort(),
        s_transtype_name,
        s_channel_name,
        s_flowno,
        s_rspcode,
        s_rspmsg
    )
    return
end
--直销银行WEB
--192.168.66.41:5555
--192.168.66.42:5555
--192.168.66.43:5555
--192.168.66.44:5555
--v1.0.5
